package First;
/*
 * Change1:修改学生成绩
 *
 * */
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import First.Window;
public class Change2 extends JFrame{
    private static final long serialVersionUID = -1928970409928880648L;

    JLabel jlnumber = new JLabel("学号：");
    JLabel jlCno = new JLabel("课程号：");
    JLabel jlGrade = new JLabel("成绩：");

    JTextField jtnumber = new JTextField("",20);
    JTextField jtCno = new JTextField("",20);
    JTextField jtGrade = new JTextField("",20);

    JButton buttonchange = new JButton("修改");
    JButton buttonreturn = new JButton("返回");

    public Change2() {
        JPanel jpnumber = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpbirthday = new JPanel();
        JPanel jpdepartment = new JPanel();
        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jpnumber.add(jlnumber);
        jpnumber.add(jtnumber);

        jpname.add(jlCno);
        jpname.add(jtCno);

        jpsex.add(jlGrade);
        jpsex.add(jtGrade);

        jpforbutton.add(buttonchange);
        jpforbutton.add(buttonreturn);

        buttonchange.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String number = jtnumber.getText();
                String Cno = jtCno.getText();
                String Grade = jtGrade.getText();

                Connection conn = null;
                ResultSet res = null;
                Statement stat = null;

                String sql = "SELECT Sno,Cno,Grade FROM SC;";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");

                }catch(Exception d){
                    System.out.println("jdbc fall");
                    d.printStackTrace();
                }
                try{
                    conn=DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT%2B8",
                            "root",
                            "12345678");
                    stat=conn.createStatement();
                    res=stat.executeQuery(sql);
                    //change
                    //change end
                    while (res.next())
                        if (res.getString(1).equals(jtnumber.getText())) {
                            try {
                                Class.forName("com.mysql.cj.jdbc.Driver");
                            } catch (Exception d) {
                                System.out.println("jdbc fall");
                                d.printStackTrace();
                            }
                            String sql2 = "UPDATE SC SET Cno='" + Cno + "' WHERE Sno='" + jtnumber.getText() + "'";
                            String sql3 = "UPDATE SC SET Grade='" + Grade + "' WHERE Sno='" + jtnumber.getText() + "'";
                            try {
                                conn = DriverManager.getConnection(
                                        "jdbc:mysql://localhost:3306/mydata?serverTimezone=GMT%2B8",
                                        "root",
                                        "12345678");
                                stat = conn.createStatement();
                                stat.executeUpdate(sql2);
                                stat.executeUpdate(sql3);
                            } catch (SQLException g) {
                                // TODO Auto-generated catch block
                                g.printStackTrace();
                            }
                            try {
                                JOptionPane.showMessageDialog(null, "修改成功！", "提示",JOptionPane.PLAIN_MESSAGE);
                                stat.close();
                                conn.close();
                            } catch (SQLException ar) {
                                ar.printStackTrace();
                            }

                            break;
                        }
                }catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();


                }
                finally{
                    try{
                        conn.close();
                    }catch(SQLException ar){
                        ar.printStackTrace();
                    }
                }
            }
        });

        buttonreturn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dispose();
            }
        });

        this.setTitle("修改学生信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirthday);
        this.add(jpdepartment);
        this.add(jpforbutton);
        this.setLocation(400,300);
        this.setSize(350,300);
        this.setVisible(true);
    }
}
